package com.tangcco.lockwell.configuration.spring.security;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.tangcco.lockwell.base.SystemCode;
import com.tangcco.lockwell.domain.UserEventLog;
import com.tangcco.lockwell.domain.UserToken;
import com.tangcco.lockwell.event.UserEvent;
import com.tangcco.lockwell.service.UserEventLogService;
import com.tangcco.lockwell.service.UserService;
import com.tangcco.lockwell.service.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;


/**
 * @version 3.5.0
 * @description: 登录成功返回
 * @date 2021/12/25 9:45
 */
@Component
public class RestAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    private final ApplicationEventPublisher eventPublisher;
    private final UserService userService;
    private final UserTokenService userTokenService;
    private final UserEventLogService userEventLogService;

    /**
     * Instantiates a new Rest authentication success handler.
     *
     * @param eventPublisher the event publisher
     * @param userService    the user service
     */
    @Autowired
    public RestAuthenticationSuccessHandler(ApplicationEventPublisher eventPublisher, UserService userService,UserTokenService userTokenService, UserEventLogService userEventLogService) {
        this.eventPublisher = eventPublisher;
        this.userService = userService;
        this.userTokenService = userTokenService;
        this.userEventLogService = userEventLogService;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Object object = authentication.getPrincipal();
        if (null != object) {
            User springUser = (User) object;
            com.tangcco.lockwell.domain.User user = userService.getUserByUserName(springUser.getUsername());
            if (null != user) {
                UserToken userToken = userTokenService.insertUserToken(user);

                UserEventLog userEventLog = new UserEventLog();
                userEventLog.setUserId(user.getUserId());
                userEventLog.setContent("登录了系统");
                userEventLog.setUserId(user.getUserId());
                userEventLog.setUserName(user.getUserName());
                userEventLog.setRealName(user.getRealName());
                userEventLog.setCreateTime(DateUtil.formatDateTime(new Date()));
                userEventLogService.insert(userEventLog);


                com.tangcco.lockwell.domain.User newUser = new com.tangcco.lockwell.domain.User();
                newUser.setUserName(user.getUserName());
                newUser.setUserId(user.getUserId());
                newUser.setImagePath(user.getImagePath());
                newUser.setToken(userToken.getToken());
                newUser.setRole(user.getRole());
                RestUtil.response(response, SystemCode.OK.getCode(), SystemCode.OK.getMessage(), newUser);
            }
        } else {
            RestUtil.response(response, SystemCode.UNAUTHORIZED.getCode(), SystemCode.UNAUTHORIZED.getMessage());
        }
    }
}
